package com.tiepbm.cucphamgiadinh.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.tiepbm.cucphamgiadinh.R;
import com.tiepbm.cucphamgiadinh.adapter.ListChapAdapter;
import com.tiepbm.cucphamgiadinh.util.MyPreferenceManager;

public class HomeActivity extends Activity implements IScreen {

	private ListView lsv;
	private ListChapAdapter adapter;
	private ArrayList<String> items = new ArrayList<String>();
	private Button btCurrentChap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		initView();
		implementListener();
		fillData();
	}

	private void loadAssetTextAsString(Context context, String name) {
		BufferedReader in = null;
		try {
			InputStream is = context.getAssets().open(name);
			in = new BufferedReader(new InputStreamReader(is));
			String str;
			while ((str = in.readLine()) != null) {
				// System.out.println(str);
				items.add(str);
			}
			// return buf.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}

		// return null;
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		lsv = (ListView) findViewById(R.id.listView1);
		btCurrentChap = (Button) findViewById(R.id.button1);
	}

	@Override
	public void implementListener() {
		// TODO Auto-generated method stub

		btCurrentChap.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MyPreferenceManager mypreference = MyPreferenceManager
						.getInstance(HomeActivity.this);
				int chap = mypreference.getChapter();
				Intent intent = new Intent(HomeActivity.this, ReadActivity.class);
				Bundle rb = new Bundle();
				rb.putInt("chap", chap);
				intent.putExtras(rb);
				startActivity(intent);
			}
		});
		lsv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				System.out.println(position);
				Intent intent = new Intent(HomeActivity.this, ReadActivity.class);
				Bundle rb = new Bundle();
				rb.putInt("chap", position + 1);
				intent.putExtras(rb);
				startActivity(intent);
			}
		});
	}

	@Override
	public void fillData() {
		// TODO Auto-generated method stub
		loadAssetTextAsString(getApplicationContext(), "cpgd/menu.txt");
		MyPreferenceManager mypre = MyPreferenceManager.getInstance(this);
		mypre.setChaperMax(items.size());
		adapter = new ListChapAdapter(this, items);
		lsv.setAdapter(adapter);
	}
}
